package com.smzd.framework.oauth2.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.BaseClientDetails;

import com.smzd.core.domain.IntIdEntity;
import com.smzd.framework.annotation.ModuleGroup;
import com.smzd.framework.security.domain.Authority;

@ModuleGroup("oauth2")
public class ClientApp extends BaseClientDetails implements IntIdEntity {
    private static final long serialVersionUID = 7873523072258947888L;
    private static final Collection<String> AUTHORIZED_GRANT_TYPES = new HashSet<String>();
    private static final Set<String> SCOPES = new HashSet<String>();

    static {
        AUTHORIZED_GRANT_TYPES.add("authorization_code");
        AUTHORIZED_GRANT_TYPES.add("implicit");
        SCOPES.add("read");
        SCOPES.add("write");
    }

    private Integer id;
    private String name;
    private String description;
    private boolean disabled;
    /**
     * resources: 应用绑定的资源为对象Resource列表。当获取所有资源ID列表时，从该对象列表中获取。
     */
    private List<Resource> resources;
    private List<Authority> clientAuths;

    public ClientApp() {
        super();
        this.setAuthorizedGrantTypes(AUTHORIZED_GRANT_TYPES);
        this.setScope(SCOPES);
        this.setAccessTokenValiditySeconds(60 * 60 * 24 * 15);
    }

    @Override
    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public Integer getId() {
        return id;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isDisabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public List<Resource> getResources() {
        return resources;
    }

    public void setResources(List<Resource> resources) {
        this.resources = resources;
    }

    @Override
    public Set<String> getResourceIds() {
        if (resources == null) {
            return Collections.<String> emptySet();
        }
        Set<String> resourceIds = new HashSet<String>();
        for (Resource resource : resources) {
            resourceIds.add(resource.getResourceId());
        }
        return resourceIds;
    }

    @Override
    public void setResourceIds(Collection<String> resourceIds) {
        if (resourceIds == null || resourceIds.size() == 0) {
            resources = null;
        } else {
            resources = new ArrayList<Resource>();
            for (String resourceId : resourceIds) {
                Resource resource = new Resource();
                resource.setResourceId(resourceId);
                resources.add(resource);
            }
        }
    }

    public List<Authority> getClientAuths() {
        return clientAuths;
    }

    public void setClientAuths(List<Authority> clientAuths) {
        this.clientAuths = clientAuths;
        Collection<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
        for (Authority authority : clientAuths) {
            authorities.add(authority);
        }
        super.setAuthorities(authorities);
    }
}
